package cn.com.lyj6851.modules.edu.service.impl;

import cn.com.lyj6851.common.service.impl.BaseServiceImpl;
import cn.com.lyj6851.dozer.service.IGenerator;
import cn.com.lyj6851.enums.CartTypeEnum;
import cn.com.lyj6851.enums.OrderInfoEnum;
import cn.com.lyj6851.modules.cart.domain.YdyStoreCart;
import cn.com.lyj6851.modules.cart.service.mapper.StoreCartMapper;
import cn.com.lyj6851.modules.edu.domain.EduCourse;
import cn.com.lyj6851.modules.edu.service.EduCourseService;
import cn.com.lyj6851.modules.edu.service.EduStoreCartService;
import cn.com.lyj6851.modules.edu.vo.GoodsVo;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @author Administrator
 */
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class EduStoreCartServiceImpl extends BaseServiceImpl<StoreCartMapper, YdyStoreCart> implements EduStoreCartService {

    private final EduCourseService eduCourseService;
    private final IGenerator generator;

    @Override
    public List<GoodsVo> getUserCartList(Long uid, String cartIds, Integer status) {
        QueryWrapper<YdyStoreCart> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(YdyStoreCart::getUid, uid)
                .eq(YdyStoreCart::getIsPay, OrderInfoEnum.PAY_STATUS_0.getValue())
                .orderByDesc(YdyStoreCart::getId);
        if (status == null) {
            wrapper.lambda().eq(YdyStoreCart::getIsNew, CartTypeEnum.NEW_0.getValue());
        }
        if (StrUtil.isNotEmpty(cartIds)) {
            wrapper.lambda().in(YdyStoreCart::getId, Arrays.asList(cartIds.split(",")));
        }
        List<YdyStoreCart> carts = baseMapper.selectList(wrapper);
        List<GoodsVo> goodsVoList = new ArrayList<>();
        for (YdyStoreCart storeCart:carts ) {
            EduCourse eduCourse = null;
            eduCourse = ObjectUtil.clone(eduCourseService.getById(storeCart.getProductId()));

            GoodsVo goodsVo = generator.convert(storeCart,GoodsVo.class);
            goodsVo.setGoodsId(storeCart.getProductId());
            goodsVo.setCourse(eduCourse);
            goodsVo.setUserId(uid);
            goodsVo.setId(storeCart.getId());
            goodsVo.setGoodsNum(storeCart.getCartNum());
            goodsVoList.add(goodsVo);
        }

        return goodsVoList;
    }
}
